﻿using OneWeb.Repository;
using OneWeb.ViewModels;
using PagedList;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.Mvc;

namespace OneWeb.Controllers
{
    /// <summary>
    /// 文章
    /// </summary>
    [RoutePrefix("article")]
    public class ArticleController : Controller
    {
        ArticleRepository essayRepository = new ArticleRepository();

        private int pageSize = 10;

        // GET: Essay
        public ActionResult Index(int? page = 1)
        {
            ArticleListPageViewModel viewModel = new ArticleListPageViewModel();
            viewModel.Articles = essayRepository.GetAll().ToPagedList(page ?? 1, pageSize);
            return View(viewModel);
        }

        [Route("details")]
        public ActionResult Details(int? id)
        {
            ArticleDetailViewModel viewModel = new ArticleDetailViewModel();
            if (id == null)
            {
                viewModel.CurrentArticle = essayRepository.GetToday();
                viewModel.PreviousArticle = essayRepository.GetPrevious();
                viewModel.NextArticle = essayRepository.GetNext();
                //throw new HttpException(404, "Not Found");
            }
            else
            {
                viewModel.CurrentArticle = essayRepository.GetToday((int)id);
                viewModel.PreviousArticle = essayRepository.GetPrevious((int)id);
                viewModel.NextArticle = essayRepository.GetNext((int)id);
            }
            return View(viewModel);
        }
    }
}